import Vue from 'vue'
import Vuex from 'vuex'
import axios from "axios";

Vue.use(Vuex)
//响应组件的动作
const actions = {
    // 获取用户信息
    topology(context, value) {
        return new Promise((resolve, reject) => {
            axios.get('/list/nodes').then(res => {
                    console.log('actions-topology return data: ', res)

                    const myD = res.data.map(node => {
                        return {
                            label: node['node-id'] + '(' + node['netconf-node-topology:host'] + ')',
                            status: node['netconf-node-topology:connection-status'],
                            children: [{
                                label: "机架01",
                                value: 2,
                                children: [
                                    {
                                        label: "槽道-1 OTN2X8",
                                        value: 3,
                                        children: [
                                            {label: "端口-1", value: 4, protocol: 'ETH'},
                                            {label: "端口-2", value: 4},
                                            {label: "端口-3", value: 4},
                                            {label: "端口-4", value: 4},
                                            {label: "端口-5", value: 4},
                                        ],
                                    },
                                    {
                                        label: "槽道-2 OTN2X8",
                                        value: 3,
                                        children: [
                                            {label: "端口-1", value: 4},
                                            {label: "端口-2", value: 4},
                                            {label: "端口-3", value: 4},
                                            {label: "端口-4", value: 4},
                                            {label: "端口-5", value: 4},
                                            {label: "端口-6", value: 4},
                                        ],
                                    },
                                    {
                                        label: "槽道-3 OTN2X8",
                                        value: 3,
                                        children: [
                                            {label: "端口-1", value: 4},
                                            {label: "端口-2", value: 4},
                                            {label: "端口-3", value: 4},
                                            {label: "端口-4", value: 4},
                                            {label: "端口-5", value: 4},
                                            {label: "端口-6", value: 4},
                                        ],
                                    },
                                    {
                                        label: "槽道-4 OTN2X8",
                                        children: [
                                            {label: "端口-1", value: 4},
                                            {label: "端口-2", value: 4},
                                            {label: "端口-3", value: 4},
                                            {label: "端口-4", value: 4},
                                            {label: "端口-5", value: 4},
                                            {label: "端口-6", value: 4},
                                        ],
                                    },
                                    {
                                        label: "槽道-5 OTN2X8",
                                        value: 3,
                                        children: [
                                            {label: "端口-1", value: 4},
                                            {label: "端口-2", value: 4},
                                            {label: "端口-3", value: 4},
                                            {label: "端口-4", value: 4},
                                            {label: "端口-5", value: 4},
                                            {label: "端口-6", value: 4},
                                        ],
                                    },
                                    {
                                        label: "槽道-6 OTN2X8",
                                        value: 3,
                                        children: [
                                            {label: "端口-1", value: 4},
                                            {label: "端口-2", value: 4},
                                            {label: "端口-3", value: 4},
                                            {label: "端口-4", value: 4},
                                            {label: "端口-5", value: 4},
                                            {label: "端口-6", value: 4},
                                        ],
                                    },
                                ],
                            }]
                        }
                    })
                    console.log('我转换好的json对象： ', myD)


                    console.log('actions-topology return data: ', res)
                    context.commit('TOPOLOGY', res);
                    for (let i = 0; i < res.data.length; i++) {
                        console.log("处于连接状态的节点： ", res.data[i]['node-id'])
                        axios.get('/node/' + res.data[i]['node-id'] + '/eqs').then(
                            response => {
                                state.topoData.push(
                                    {
                                        label: res.data[i]['node-id'],
                                        value: 1,
                                        children: [{
                                            label: "机架01",
                                            value: 2,
                                            children: [
                                                {
                                                    label: "槽道-1 OTN2X8",
                                                    value: 3,
                                                    children: [
                                                        {label: "端口-1", value: 4},
                                                        {label: "端口-2", value: 4},
                                                        {label: "端口-3", value: 4},
                                                        {label: "端口-4", value: 4},
                                                        {label: "端口-5", value: 4},
                                                    ],
                                                },
                                                {
                                                    label: "槽道-2 OTN2X8",
                                                    value: 3,
                                                    children: [
                                                        {label: "端口-1", value: 4},
                                                        {label: "端口-2", value: 4},
                                                        {label: "端口-3", value: 4},
                                                        {label: "端口-4", value: 4},
                                                        {label: "端口-5", value: 4},
                                                        {label: "端口-6", value: 4},
                                                    ],
                                                },
                                                {
                                                    label: "槽道-3 OTN2X8",
                                                    value: 3,
                                                    children: [
                                                        {label: "端口-1", value: 4},
                                                        {label: "端口-2", value: 4},
                                                        {label: "端口-3", value: 4},
                                                        {label: "端口-4", value: 4},
                                                        {label: "端口-5", value: 4},
                                                        {label: "端口-6", value: 4},
                                                    ],
                                                },
                                                {
                                                    label: "槽道-4 OTN2X8",
                                                    children: [
                                                        {label: "端口-1", value: 4},
                                                        {label: "端口-2", value: 4},
                                                        {label: "端口-3", value: 4},
                                                        {label: "端口-4", value: 4},
                                                        {label: "端口-5", value: 4},
                                                        {label: "端口-6", value: 4},
                                                    ],
                                                },
                                                {
                                                    label: "槽道-5 OTN2X8",
                                                    value: 3,
                                                    children: [
                                                        {label: "端口-1", value: 4},
                                                        {label: "端口-2", value: 4},
                                                        {label: "端口-3", value: 4},
                                                        {label: "端口-4", value: 4},
                                                        {label: "端口-5", value: 4},
                                                        {label: "端口-6", value: 4},
                                                    ],
                                                },
                                                {
                                                    label: "槽道-6 OTN2X8",
                                                    value: 3,
                                                    children: [
                                                        {label: "端口-1", value: 4},
                                                        {label: "端口-2", value: 4},
                                                        {label: "端口-3", value: 4},
                                                        {label: "端口-4", value: 4},
                                                        {label: "端口-5", value: 4},
                                                        {label: "端口-6", value: 4},
                                                    ],
                                                },
                                            ],
                                        }]
                                    });
                            },
                            error => {
                                console.log("失败！！", error.message)
                                // this.$message.error(error.message);
                            }
                        )
                    }
                    resolve(res)
                }
            ).catch(error => {
                reject(error)
            })
        })
    },
    jiaOdd(context, value) {
        console.log('actions中的jiaOdd被调用了')
        if (context.state.sum % 2) {
            context.commit('JIA', value)
        }
    },

    test() {

        const data1 = {
            totalEmelents: 100,
            list: [
                {id: 0, name: "a"},
                {id: 1, name: "b"},
                {id: 2, name: "c"},
            ]
        }

        const myList = data1.list.map(item => {
            return {
                key: item.id,
                value: item.name
            }
        });
        console.log('***************************************************');
        console.log(...data1.list)
        console.log(...myList)
        console.log('***************************************************');

        function renameKeys(obj, newKeys) {
            const keyValues = Object.keys(obj).map(key => {
                const newKey = newKeys[key] || key;
                return {[newKey]: obj[key]};
            });
            return Object.assign({}, ...keyValues);
        }

        //方法1
        console.log('-----------------方法1-----------------');
        const obj = {a: "1", b: "2"};
        const newKeys1 = {a: "A23", b: "Cbc"};
        const renamedObj1 = renameKeys(obj, newKeys1);
        console.log(renamedObj1);// {A:"1", b:"2"}
        console.log('--------------------------------------');

        //方法2
        // console.log('-----------------方法2---------------');
        // 使用lodash的_.mapKeys()函数
        // let user = {name: "Andrew", id: 25, reported: false};
        // let renamed = mapKeys(user, function (value, key) {
        //     return key + "_" + user.id;
        // });
        // console.log(renamed);
        // console.log('--------------------------------------');

        //方法3
        console.log('-----------------方法3---------------');
        let object = {name: "Ane", id: 25, reported: false};
        // let object = [{name: "Ane", id: 25, reported: false}, {name: "Bod", id: 29, reported: true},];
        console.log('要转化的数据：')
        console.log(object);
        let str = JSON.stringify(object);
        // 暂时有问题，未改正
        // for (let i = 0; i < str.length; i++) {
        //     str[i] = str[i].replace('name', 'mingzi');
        //     str[i] = str[i].replace('id', 'number');
        // }

        //只能修改数组中的第一个对象
        str = str.replace('name', 'mingzi');
        str = str.replace('id', 'number');
        object = JSON.parse(str);
        console.log('转换后的数据：')
        console.log(object)
        console.log('--------------------------------------');

        //方法4
        console.log('-----------------方法4---------------');

        function renameObjectKey(oldObj, oldName, newName) {
            const newObj = {};
            Object.keys(oldObj).forEach(key => {
                const value = oldObj[key];
                if (key === oldName) {
                    newObj[newName] = value;
                } else {
                    newObj[key] = value;
                }
            });
            return newObj;
        }

        let obj01 = {name: "Ane", id: 25, reported: false};
        console.log('要转化的数据：')
        console.log(obj01);
        let myobj1 = renameObjectKey(obj01, 'name', 'mingzi');
        console.log('转换后的数据：')
        console.log(myobj1)

        console.log('--------------------------------------');
        //方法5
        console.log('-----------------方法5---------------');
        let data = {key1: "value1", key2: "value2", key3: "value3"};
        let keyMap = {key1: "firstkey", key2: "secondkey", key3: "thirdkey"};
        console.log('要转化的数据：')
        console.log(data);
        let mappedData = Object.keys(keyMap).reduce((obj, k) => Object.assign(obj, {[keyMap[k]]: data[k]}), {});
        console.log('转化好的数据：')
        console.log(mappedData);
        console.log('--------------------------------------');


        var obj2 = {
            "_id": "5078c3a803ff4197dc81fbfb",
            "email": "user1@gmail.com",
            "image": "some_image_url",
            "name": "Name 1"
        };
        var new_key = "id";
        var old_key = "_id";
        //最小差异化替换key
        Object.keys(obj2).forEach(key => {
            if (key === old_key) {
                obj2[new_key] = obj2[key];
                delete obj2[key];
            } else {
                obj2[`_${key}`] = obj2[key];
                delete obj2[key];
                obj2[`${key}`] = obj2[`_${key}`];
                delete obj2[`_${key}`];
            }
        });
        console.log('---------新的json：', obj2)
        //需求是将b根据a的值替换对象中的key值
        let a = ["code", "name", "date", "font", "hello"];
        let b = [{m1: 2, m2: 3, m3: 4, m4: 8}, {c1: 2, c2: 3, c3: 4}, {1: 'knn', 2: 3, 3: 4, 15: 9}];

        //[{'code':2,'name':3,'date':4},{'code':2,'name':3,'date':4},{'code':2,'name':3,'date':4}];

        let c = b.map((item, index, arr) => {
            // debugger;
            let obj = {};

            const m = arr === b ? '是的' : 'no';
            console.log('我转换的json对象arr是否和b的一致', m)
            for (let i in item) {
                obj[a[i - 1]] = item[i];
            }
            return obj;
        });
        console.log('我测试的json： ', c);//  [{'code':2,'name':3,'date':4},{'code':2,'name':3,'date':4},{'code':2,'name':3,'date':4}];


    }

}
//操作数据
const mutations = {
    TOPOLOGY(state, value) {
        console.log('mutations中的TOPOLOGY被调用了')
        // console.log('mutations-topology data: ', state, value)
        // // state.topoData = value
        // console.log('state.topoData', state.topoData)
    },
    CLICKNODE(state, value) {
        console.log('点击得到的公共数据', value)
        state.clickNode = value;
        console.log('点击更改得到的公共数据-包装后的', state.clickNode)

    }
}
//存储数据
const state = {
    treeData: [
        {
            label: "全局视图",
            value: 0,
            children: [
                {
                    label: "OTNA(192.168.12.112)",
                    value: 1,
                    name: "hclink3",
                    children: [
                        {
                            label: "机架01",
                            value: 2,
                            children: [
                                {
                                    label: "槽道-1 OTN2X8",
                                    value: 3,
                                    name: "",
                                    children: [
                                        {label: "端口-1", name: "", value: 4},
                                        {label: "端口-2", value: 4},
                                        {label: "端口-3", value: 4},
                                        {label: "端口-4", value: 4},
                                        {label: "端口-5", value: 4},
                                    ],
                                },
                                {
                                    label: "槽道-2 OTN2X8",
                                    value: 3,
                                    children: [
                                        {label: "端口-1", value: 4},
                                        {label: "端口-2", value: 4},
                                        {label: "端口-3", value: 4},
                                        {label: "端口-4", value: 4},
                                        {label: "端口-5", value: 4},
                                        {label: "端口-6", value: 4},
                                    ],
                                },
                                {
                                    label: "槽道-3 OTN2X8",
                                    value: 3,
                                    children: [
                                        {label: "端口-1", value: 4},
                                        {label: "端口-2", value: 4},
                                        {label: "端口-3", value: 4},
                                        {label: "端口-4", value: 4},
                                        {label: "端口-5", value: 4},
                                        {label: "端口-6", value: 4},
                                    ],
                                },
                                {
                                    label: "槽道-4 OTN2X8",
                                    children: [
                                        {label: "端口-1", value: 4},
                                        {label: "端口-2", value: 4},
                                        {label: "端口-3", value: 4},
                                        {label: "端口-4", value: 4},
                                        {label: "端口-5", value: 4},
                                        {label: "端口-6", value: 4},
                                    ],
                                },
                                {
                                    label: "槽道-5 OTN2X8",
                                    value: 3,
                                    children: [
                                        {label: "端口-1", value: 4},
                                        {label: "端口-2", value: 4},
                                        {label: "端口-3", value: 4},
                                        {label: "端口-4", value: 4},
                                        {label: "端口-5", value: 4},
                                        {label: "端口-6", value: 4},
                                    ],
                                },
                                {
                                    label: "槽道-6 OTN2X8",
                                    value: 3,
                                    children: [
                                        {label: "端口-1", value: 4},
                                        {label: "端口-2", value: 4},
                                        {label: "端口-3", value: 4},
                                        {label: "端口-4", value: 4},
                                        {label: "端口-5", value: 4},
                                        {label: "端口-6", value: 4},
                                    ],
                                },
                            ],
                        },
                    ],
                },
            ],

        },
    ],
    node: [],
    listInfos: [{
        'node-id': '',
        'netconf-node-topology:connection-status': '',
        'netconf-node-topology:port': '',
        'netconf-node-topology:host': '',
    }],
    clickNode: {
        id: '',
        label: '',
        level: '',
        data: {
            label: '',
            value: ''
        },
        parent: {},
        children: [],
        protocol: '',
        status: ''
    },
    topoData: [],
}

//准备getters——用于将state中的数据进行加工
const getters = {

    clickNode() {

        console.log("现在的拓扑结构：", state.topoData)


    },
    bigSum(state) {
        return state.sum * 10
    }
}


//创建store
const store = new Vuex.Store({
    actions,
    mutations,
    state,
    getters
})

export default store